<template>
  <div style="padding-bottom: 20px;padding-right: 20px;">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px" :label-position="'left'">
      <el-form-item label="项目名称" prop="project_name">
        <el-input
          v-model="queryParams.project_name"
          style="width: 130px;"
          class="wenbnen"
          placeholder="项目名称"
          clearable
          size="mini"
        />
      </el-form-item>

      <el-form-item label="入库编号" prop="warehousing_id">
        <el-input
          v-model="queryParams.warehousing_id"
          style="width: 130px;"
          class="wenbnen"
          placeholder="入库编号"
          clearable
          size="mini"
        />
      </el-form-item>
      <el-form-item label="入库状态" prop="status">
        <el-select v-model="queryParams.status" style="width: 130px;" class="wenbnen" placeholder="入库状态" clearable size="mini">
          <el-option label="未入库" value="0" />
          <el-option label="全部入库" value="1" />
          <el-option label="部分入库" value="2" />
        </el-select>
      </el-form-item>

      <el-form-item label="日期范围" prop="date_select_type">
        <el-radio-group v-model="date_select_type">
          <el-radio :label="'今天'">今天</el-radio>
          <el-radio :label="'近7天'">近7天</el-radio>
          <el-radio :label="'近30天'">近30天</el-radio>
          <el-radio :label="'三个月前'">三个月前</el-radio>
          <el-radio :label="'自定义'">自定义</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item
        v-show="date_select_type === '自定义'"
        label="创建日期"
      >
        <el-date-picker
          ref="pick1"
          v-model="queryParams.created_at"
          style="font-size: 12px;"
          value-format="yyyy-MM-dd"
          type="daterange"
          size="mini"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      ref="table1"
      v-loading="loading"
      class="table-wrap"
      :data="tableData"
      row-key="id"
      border
      fit
      header-cell-class-name="header-cell-color"
    >
      <el-table-column
        v-for="(item, index) in bindTableColumns"
        :key="`colTableHeaderTit_${index}`"
        class-name="allowDrag"
        show-overflow-tooltip
        :prop="tableHeaderTit[index].field"
        :label="tableHeaderTit[index].label"
        align="center"
      />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.statusStr == '全部入库' && scope.row.edi_return == '未回传'"
            size="mini"
            type="success"
            @click="handleReturn(scope.row)">回传</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" small :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.limit" @pagination="getList" />
    <!-- 新增/修改 -->
  </div>
</template>

<script>
import { ediDataReturn } from '@/api/edi'
import {parseTime} from "@/utils";
import {warehousingList} from "@/api/storehouse";
export default {
  name: "Warehousing",
  data() {
    return {
      date_select_type: '近30天',
      total: 0,
      queryParams: {
        page: 1,
        limit: 10,
        warehousing_id: undefined,
        status: undefined,
        customer_order_type: undefined
      },
      tableData: [],
      tableHeaderTit: [
        { label: '入库编号', field: 'warehousing_id', minWidth: 50, isShow: true },
        { label: '项目/委托方', field: 'project_name', minWidth: 50, isShow: true },
        { label: '客户业务单号', field: 'customer_business_number', minWidth: 50, isShow: true },
        { label: '商品总数量', field: 'goods_number', minWidth: 50, isShow: true },
        { label: '入库数量', field: 'actual_number', minWidth: 50, isShow: true },
        { label: '创建时间', field: 'created_at', minWidth: 50, isShow: true },
        { label: '入库状态', field: 'statusStr', minWidth: 50, isShow: true },
        { label: '发布人', field: 'pub_name', minWidth: 50, isShow: true },
        { label: 'edi状态', field: 'edi_return', isShow: true, minWidth: 50 }
      ],
      colTableHeaderTit: [
        { label: '入库编号', field: 'warehousing_id', minWidth: 50, isShow: true },
        { label: '项目/委托方', field: 'project_name', minWidth: 50, isShow: true },
        { label: '客户业务单号', field: 'customer_business_number', minWidth: 50, isShow: true },
        { label: '商品总数量', field: 'goods_number', minWidth: 50, isShow: true },
        { label: '入库数量', field: 'actual_number', minWidth: 50, isShow: true },
        { label: '创建时间', field: 'created_at', minWidth: 50, isShow: true },
        { label: '入库状态', field: 'statusStr', minWidth: 50, isShow: true },
        { label: '发布人', field: 'pub_name', minWidth: 50, isShow: true },
        { label: 'edi状态', field: 'edi_return', isShow: true, minWidth: 50 }
      ],
    }
  },
  created() {
    this.getList()
  },
  computed: {
    bindTableColumns() { // 表格展示的字段
      const res = this.colTableHeaderTit.filter((v) => {
        return v.isShow == true
      })
      this.tableHeaderTit = JSON.parse(JSON.stringify(res))
      return res
    },
    checkedTableColumns: {
      get() {
        return this.bindTableColumns.map((v) => v.label)
      },
      set(value) {
        this.nimabi = false
        this.colTableHeaderTit.forEach((v) => {
          if (value.includes(v.label)) {
            v.isShow = true
          } else {
            v.isShow = false
          }
        })
      }
    }
  },
  watch: {
    'date_select_type': {
      handler(val) {
        switch (val) {
          case '今天':
            var end = new Date()
            var start = new Date()
            start = parseTime(start.getTime(), '{y}-{m}-{d}')
            end = parseTime(end.getTime() + 3600 * 1000 * 24, '{y}-{m}-{d}')
            this.$set(this.queryParams, 'created_at', [start, end])
            break
          case '近7天':
            var end = new Date()
            var start = new Date()
            start = parseTime(start.getTime() - 3600 * 1000 * 24 * 6, '{y}-{m}-{d}')
            end = parseTime(end.getTime() + 3600 * 1000 * 24, '{y}-{m}-{d}')
            this.$set(this.queryParams, 'created_at', [start, end])
            break
          case '近30天':
            var end = new Date()
            var start = new Date()
            start = parseTime(start.getTime() - 3600 * 1000 * 24 * 29, '{y}-{m}-{d}')
            end = parseTime(end.getTime() + 3600 * 1000 * 24, '{y}-{m}-{d}')
            this.$set(this.queryParams, 'created_at', [start, end])
            break
          case '三个月前':
            var end = new Date()
            var start = new Date()
            end = parseTime(end.getTime() - 3600 * 1000 * 24 * 89, '{y}-{m}-{d}')
            start = parseTime(Date.parse('2000/01/01'), '{y}-{m}-{d}')
            this.$set(this.queryParams, 'created_at', [start, end])
            break
        }
      },
      deep: true, // 深度监听
      immediate: true // 初次监听即执行
    }
  },
  methods: {
    /** 查询列表 */
    getList() {
      const that = this
      this.loading = true
      warehousingList(this.queryParams).then(response => {
        if (response.code === 10000) {
          if (response.data.length > 0) {
            response.data.forEach(item => {
              if (item.status == '0') { item['statusStr'] = '未入库' }
              if (item.status == '1') { item['statusStr'] = '全部入库' }
              if (item.status == '2') { item['statusStr'] = '部分入库' }
              item['edi_return'] = item['edi_return'] == 0?'未回传':'已回传'
            })
          }
          that.tableData = response.data
          that.total = response.count
          that.tableData.forEach(item => {
            if (item.status != 0) {
              item.disabled = 'disabled'
            }
          })
          that.loading = false
        }
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        page: 1,
        limit: 10,
        warehousing_id: undefined,
        status: undefined,
        customer_order_type: undefined
      }
      this.handleQuery()
    },
    handleReturn(row){
      this.$confirm('正在进行edi操作, 请确认?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = false
        ediDataReturn({type:'入库',record_id:row.id}).then(response => {
          this.loading = false
          if (response.code == 10000) {
            this.$message({
              type: 'success',
              message: '操作成功'
            })
            this.getList()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
